#!/usr/bin/env node

const cwd = process.cwd();
const path = require('path');
const fs = require('fs');
const ejs = require('ejs');
const inquirer = require('inquirer')

inquirer.prompt([
    {
        name: 'name',
        default: 'project',
        type: 'input',
        message: '请输入项目名称'
    },
    {
        name: 'dir',
        default: 'templates',
        type: 'input',
        message: '请输入模板路径'
    }
]).then(answers => {
    const { name = 'project', dir = 'templates' } = answers;
    const temDir = path.join(cwd, dir);
    const distDir = cwd;

    fs.readdir(temDir,( err, files) => {
        if (err) return;
        console.log('files', files);
        files.forEach(item => {
            console.log('aaa', path.join(temDir, item));
            ejs.renderFile(path.join(temDir, item), answers, {}, (err, str) => {
                if (err) throw err;
                fs.writeFileSync(path.join(distDir, item), str);
            });
        });

    });
})